package com.capgemini.export.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.capgemini.excel.util.ExcelExportUtil;
import com.capgemini.export.pojo.LogisticsDataDto;
import com.capgemini.export.pojo.LogisticsQueryDto;
import com.capgemini.export.service.LogisticsService;

@Controller
@RequestMapping("/logisticDataExport")
public class LogisticsExportController {
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private LogisticsService logisticService;
	
	@RequestMapping("/export")
	public void doExport(HttpServletRequest request, HttpServletResponse response, LogisticsQueryDto queryDto) {
		List<LogisticsDataDto> data = logisticService.queryLogisticsData(queryDto);
		List<List<String>> listToExport = new LinkedList<List<String>>();
		List<String> header = new LinkedList<String>();
		if (null != data) {
			header.add("门店编号");
			header.add("门店名称");
			header.add("订单编号");
			header.add("商品名称");
			header.add("实发数量");
			header.add("发货数量");
			header.add("预计到货");
			header.add("物流公司");
			header.add("物流单号");
			header.add("实收数量");
			header.add("收获日期");
			header.add("状态");
			Iterator<LogisticsDataDto> itd = data.iterator();
			while (itd.hasNext()) {
				LogisticsDataDto item = itd.next();
				List<String> row = new LinkedList<String>();
				row.add(item.getDealerNo());
				row.add(item.getDealerName());
				row.add(item.getOrderNo());
				row.add(item.getCardName());
				row.add(item.getShipAmount());
				row.add(item.getShipDate());
				row.add(item.getEstimatedArriveDate());
				row.add(item.getExpressCom());
				row.add(item.getExpressNo());
				if (Integer.valueOf(null == item.getReceiveAmount() ? "0"
						: "".equals(item.getReceiveAmount()) ? "0" : item.getReceiveAmount()) <= Integer
								.valueOf(null == item.getShipAmount() ? "0" : "".equals(item.getShipAmount()) ? "0"
										: item.getShipAmount())) {
					row.add(item.getReceiveAmount());
				} else {
					row.add("");
				}
				row.add(item.getReceiveDate());
				if ("0".equals(item.getStatus())) {
					row.add("在途");
				} else if("1".equals(item.getStatus())) {
					row.add("已接收");
				}
				listToExport.add(row);
			}
		}
		SXSSFWorkbook wb = null;
		try {
			wb = ExcelExportUtil.generateExcel(listToExport, header);
			 response.setHeader("content-disposition","attachment;filename=cardlogistics.xlsx");
		     response.setHeader("Content-Type","application/vnd.ms-excel;charset=utf-8");
			wb.write(response.getOutputStream());

			wb.close();
			wb.dispose();
		} catch (Exception e) {
			log.error("", e);
		} finally {
			try {
				if (wb != null) {
					wb.close();
					wb.dispose();
				}
			} catch (Exception e) {
				log.error("", e);
			}
		}
	}
}
